package ligne2vie.topos.server.service;

import java.util.logging.Logger;

import javax.jdo.PersistenceManager;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * The class {@link AbstractRemoteService} provides a convenience base for all remote services managing entities on the
 * database. It provides common methods to access data and make database actions on them.
 * 
 * @author thuguerre
 * @since 1.0.0
 */
public abstract class AbstractRemoteService extends RemoteServiceServlet {

	/**
	 * {@link SummitServiceImpl}'s logger.
	 */
	private static final Logger LOGGER = Logger.getLogger(SummitServiceImpl.class.getName());

	/**
	 * Default serial UID.
	 */
	private static final long serialVersionUID = -3105667564337839364L;

	/**
	 * Convenience method returning a valid and fully configured {@link PersistenceManager} which can be used to operate
	 * easily operations on the database.
	 * 
	 * @return Valid {@link PersistenceManager}.
	 */
	protected PersistenceManager _getPM() {

		PersistenceManager pm = PMF.get().getPersistenceManager();
		return pm;
	}

	/**
	 * Allows persisting the given entity to the database.
	 * 
	 * @param entity
	 *            Entity to persist.
	 */
	public void persist(Object entity) {

		PersistenceManager pm = _getPM();

		try {

			LOGGER.fine("Persisting entity...");
			pm.makePersistent(entity);
			LOGGER.fine("entity persisted.");

		} finally {
			pm.close();
		}
	}
}
